package ch.rheintalconnection.sysventory.technicalServices.wmi;

import java.util.ArrayList;
import java.util.List;

import ch.rheintalconnection.sysventory.dto.PrinterConfigurationDto;

import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.jacob.com.EnumVariant;
import com.jacob.com.Variant;

public class PrinterConfigurationScan {
	
	public static final String QUERY = "SELECT * FROM Win32_PrinterConfiguration";
	
	private ActiveXComponent axWMI;
	
	
	public PrinterConfigurationScan(ActiveXComponent axWMI) {
		this.axWMI = axWMI;
	}
	
	public List<PrinterConfigurationDto> scan(){
		
		ArrayList<PrinterConfigurationDto> printerConfigurationDtos = new ArrayList<PrinterConfigurationDto>();
		
		Variant vCollection = axWMI.invoke("ExecQuery", new Variant(QUERY));
		EnumVariant enumVariant = new EnumVariant(vCollection.toDispatch());
		Dispatch item = null;
		
		while (enumVariant.hasMoreElements()) {
			item = enumVariant.nextElement().toDispatch();

			String version = Dispatch.call(item, "DriverVersion").toString();
			
			printerConfigurationDtos.add(new PrinterConfigurationDto(version));
		}
		return printerConfigurationDtos;	
	}
		
}
